package com.gzx.spring.apis.utils;

import com.gzx.risk.core.utils.CryptoUtils;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * 探知数据
 */
public class TanzhiUtils {
    private String apiGatway;
    private String apiKey;
    private String appSecret;
    private String version;
    private String method;
    private String reportVersion;

    public TanzhiUtils apiGatway(String apiGatway) {
        this.apiGatway = apiGatway;
        return this;
    }

    public TanzhiUtils apiKey(String apiKey) {
        this.apiKey = apiKey;
        return this;
    }

    public TanzhiUtils appSecret(String appSecret) {
        this.appSecret = appSecret;
        return this;
    }

    public TanzhiUtils version(String version) {
        this.version = version;
        return this;
    }

    public TanzhiUtils method(String method) {
        this.method = method;
        return this;
    }

    public TanzhiUtils reportVersion(String reportVersion) {
        this.reportVersion = reportVersion;
        return this;
    }

    private String exec(Map<String, String> params) {
        RestTemplate restTemplate = new RestTemplate();
        params.put("apiKey", apiKey);
        params.put("version", version);
        params.put("sign", CryptoUtils.signParam(params, appSecret));

        MultiValueMap<String, String> paramMap = new LinkedMultiValueMap<>();
        paramMap.setAll(params);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(paramMap, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(apiGatway, httpEntity, String.class);

        if (HttpStatus.OK.equals(response.getStatusCode())) {
            return response.getBody();
        }
        return "";
    }

    /**
     * 全域风险指数
     *
     * @return
     */
    public String global_risk(String name, String idCard, String phone) {
        Map<String, String> params = new HashMap<>();
        params.put("mobile", phone);
        params.put("idCard", idCard);
        params.put("trueName", name);
        params.put("method", method);
        return this.exec(params);
    }

    /**
     * 探预分e
     * @param name 姓名
     * @param idCard 身份证
     * @param phone 手机号
     * @return
     */
    public String ty_e(String name, String idCard, String phone){
        Map<String, String> params = new HashMap<>();
        params.put("mobile", phone);
        params.put("idCard", idCard);
        params.put("trueName", name);
        params.put("method", method);
        params.put("reportVersion", reportVersion);
        return this.exec(params);
    }

    /**
     * 探预分d
     * @param name 姓名
     * @param idCard 身份证
     * @param phone 手机号
     * @return
     */
    public String ty_d(String name, String idCard, String phone){
        Map<String, String> params = new HashMap<>();
        params.put("mobile", phone);
        params.put("idCard", idCard);
        params.put("trueName", name);
        params.put("method", method);
        params.put("reportVersion", reportVersion);
        return this.exec(params);
    }
}
